package com.wn.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.dao.EmpDao;
import com.wn.entity.Emp;
import com.wn.entity.PageParam;
import com.wn.entity.Role;
import com.wn.entity.Userinfo;
import com.wn.service.EmpService;
import com.wn.utils.JwtUtil;
import com.wn.utils.Rsp;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * (Emp)表服务实现类
 *
 * @author makejava
 * @since 2020-10-26 09:29:19
 */
@Service("empService")
@Transactional(rollbackFor = Exception.class)
public class EmpServiceImpl implements EmpService {
    @Resource
    private EmpDao empDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Emp queryById(String id) {
        return this.empDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Emp> queryAllByLimit(int offset, int limit) {
        return this.empDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param emp 实例对象
     * @return 实例对象
     */
    @Override
    public Emp insert(Emp emp) {
        this.empDao.insert(emp);
        return emp;
    }

    /**
     * 修改数据
     *
     * @param emp 实例对象
     * @return 实例对象
     */
    @Override
    public Emp update(Emp emp) {
        this.empDao.update(emp);
        return this.queryById(emp.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.empDao.deleteById(id) > 0;
    }

    public PageInfo<Emp> searchSomeByPage(PageParam<Emp> pageParam) {

        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Emp> newscategoryList=empDao.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<Emp> newscategoryPageInfo = new PageInfo<Emp>(newscategoryList);

            return newscategoryPageInfo;

        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Emp> newscategoryList=empDao.searchSomeByPage(pageParam.getModel());
            PageInfo<Emp> newscategoryPageInfo = new PageInfo<Emp>(newscategoryList);

            return newscategoryPageInfo;

        }

    }

    @Override
    public Rsp deleteOneTrue(String id) {
        return empDao.deleteOneTrue(id)==1 ? Rsp.success(null).msg("逻辑删除成功"):Rsp.fail().msg("逻辑删除失败");
    }

    @Override
    public Emp empSelectInfo(String id) {
        Emp emp = empDao.empSelectInfo(id);
        return emp;
    }

    @Override
    public Rsp updateId(Emp newsorder) {
        return empDao.update(newsorder)==1?Rsp.success(null).msg("修改成功"):Rsp.fail().msg("修改失败");
    }

    /**
     * 登录
     * @param loginEmp
     * @return
     */
    @Override
    public Rsp empLogin(Emp loginEmp) {
        Emp emp = empDao.empLogin(loginEmp);

        if(emp!=null){
            try {
                Role role = empDao.selectRole(emp.getRoleid().toString());
                emp.setRoleSign(role.getRoleSign());
                return Rsp.success(JwtUtil.createToken(emp));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            return Rsp.fail().msg("登录失败：账号或密码错误");
        }

        return null;
    }

    /**
     * 查询角色
     * @param id
     * @return
     */
    @Override
    public Role selectRole(String id) {
        return empDao.selectRole(id);
    }


    @Override
    public Rsp addOne(Emp newsorder) {
        newsorder.setId(UUID.randomUUID().toString().replaceAll("-", ""));
//        newscategory.setCreateTime(new Date());
            //随机生成账号
            newsorder.setEmpaccount(UUID.randomUUID().toString().substring(2,8));
            //随机生成密码
            newsorder.setEmppassword(UUID.randomUUID().toString().substring(2,8));


        return empDao.addOne(newsorder)==1?Rsp.success("添加成功"):Rsp.fail().msg("添加失败");
    }

}
